package com.yida.wy.vote.service.impl;


import com.mybatisflex.annotation.UseDataSource;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.text.Convert;
import com.yida.common.core.utils.StringUtils;
import com.yida.wy.common.constant.WyConstants;
import com.yida.wy.common.domain.WyBaseCommunity;
import com.yida.wy.common.domain.WyBaseHouse;
import com.yida.wy.common.domain.WyBaseOwner;
import com.yida.wy.common.service.IWyBaseCommunityService;
import com.yida.wy.common.service.IWyBaseOwnerService;
import com.yida.wy.common.utils.WyConvertUtil;
import com.yida.wy.vote.domain.WyMaintenanceFundsBusiness;
import com.yida.wy.vote.domain.dto.WyBaseHouseOwnerDTO;
import com.yida.wy.vote.mapper.WyBusinessDataMapper;
import com.yida.wy.vote.service.IWyBusinessDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公维金业务数据Service业务层处理
 *
 * @author lqq
 * @date 2023-03-22
 */
@Service
@UseDataSource("FileDs")
public class WyBusinessDataServiceImpl implements IWyBusinessDataService {

    @Resource
    private WyBusinessDataMapper gwjBusinessDataMapper;

    @Resource
    private IWyBaseOwnerService gwjBaseOwnerService;

    @Resource
    private IWyBaseCommunityService gwjBaseCommunityService;


    /**
     * 根据业务ID查询所有数据（楼栋、房屋、业主）
     *
     * @param bizId 业务ID
     * @return /
     */
    @Override
    public WyBaseHouseOwnerDTO getAllDataByBizId(Long bizId) {
        //查询业务小区
        WyBaseCommunity baseCommunity = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessCommunityByBizId(bizId);
        if (StringUtils.isNull(baseCommunity)) {
            throw new ServiceException("查无业务小区数据");
        }
        //查询业务房屋
        List<WyBaseHouse> baseHouses = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessHouseByBizId(bizId);
        //剔除公用户
        baseHouses.removeIf(h -> WyConstants.YES.equals(h.getPublicFlag()));
        if (StringUtils.isEmpty(baseHouses)) {
            throw new ServiceException("查无基础房屋数据");
        }
        //查询基础业主
        List<WyBaseOwner> baseOwners = new ArrayList<>();
        List<String> houseCodes = baseHouses.stream().map(WyBaseHouse::getHouseCode).collect(Collectors.toList());
        //分段查询
        List<List<String>> codeList = WyConvertUtil.partition(houseCodes, 100);
        for (List<String> code : codeList) {
            List<WyBaseOwner> owners = gwjBaseOwnerService.selectGwjBaseListByGwjBaseHouseCodes(code.toArray(new String[0]), baseCommunity.getAreaId());
            if (StringUtils.isNotEmpty(owners)) {
                baseOwners.addAll(owners);
            }
        }
        if (StringUtils.isEmpty(baseOwners)) {
            throw new ServiceException("查无基础业主数据");
        }
        baseOwners.forEach(o -> {
            if (StringUtils.isAnyBlank(o.getOwnerPhone(), o.getOwnerName())) {
                throw new ServiceException("请完善业主信息");
            }
        });
        WyBaseHouseOwnerDTO dto = new WyBaseHouseOwnerDTO();
        dto.setBaseHouses(baseHouses);
        dto.setBaseOwners(baseOwners);
        dto.setBaseCommunity(baseCommunity);
        return dto;
    }

    /**
     * 校验业务状态
     *
     * @param bizId 业务楼栋主键
     */
    @Override
    public WyMaintenanceFundsBusiness checkMaintenanceFundsBusinessStatus(Long bizId) {
        WyMaintenanceFundsBusiness business = gwjBusinessDataMapper.selectGwjMaintenanceFundsBusinessByBizId(bizId);
        if (StringUtils.isNull(business)) {
            throw new ServiceException("查无业务数据");
        }
        if (Convert.toInt(WyConstants.YES) != business.getVoteFlag()) {
            throw new ServiceException("当前业务流程无法创建投票");
        }
        return business;
    }

    /**
     * 查询基础数据（小区信息）
     *
     * @param id 基础数据（小区信息）主键
     * @return 基础数据（小区信息）
     */
    @Override
    public WyBaseCommunity selectGwjBaseCommunityByGwjBaseCommunityId(Long id) {
        return gwjBaseCommunityService.getById(id);
    }
}
